/**
 * Created by fq on 2018/1/23 0023.
 */
'use strict';
angular.module('indexApp')
    .controller('registeredCtrl', function ($scope, $state, $rootScope, $http, $stateParams, $timeout, $interval) {
        var vm = this;
        vm.http = $rootScope.http;

        $scope.state = $state;
        $(document).ready(function () {
            $("#dtBox").DateTimePicker(
                {
                    dateFormat: "yyyy-MM-dd",
                });
        });

        //发送验证码
        //倒计时
        vm.timer = false;
        vm.timeout = 120000;
        vm.timerCount = vm.timeout / 1000;
        vm.text = "获取验证码";
        vm.sendSms = function (num) {
            // $rootScope.alert($rootScope.id);
            // $rootScope.alert(num);
            if (num == '' || num == undefined || num == null) {
                $rootScope.alert('请填写正确的手机号')
            }
            else {
                $http({
                    method: 'post',
                    url: vm.http + 'send_sms',
                    data: {
                        mobile: num,
                        user_id: $rootScope.id
                    }
                })
                    .then(function successCallback(res) {
                        if (res.data.code == 0) {
                            // $rootScope.alert(res.data.msg);
                            // vm.data.code = res.data.data.code;
                            vm.showTimer = true;
                            vm.timer = true;
                            vm.text = "秒后重新获取";
                            var counter = $interval(function () {
                                vm.timerCount = vm.timerCount - 1;
                            }, 1000);
                            $timeout(function () {
                                vm.text = "获取验证码";
                                vm.timer = false;
                                $interval.cancel(counter);
                                vm.showTimer = false;
                                vm.timerCount = vm.timeout / 1000;
                            }, vm.timeout);
                        }
                        else {
                            $rootScope.alert(res.data.msg)
                        }
                    }, function errorCallback(res) {// 请求失败执行代码
                        $rootScope.alertError();
                    });
            }

        };


        vm.registered = function () {
            vm.data.activity_id = $stateParams.id;
            vm.data.user_id = $rootScope.id;
            vm.data.invite_id=localStorage.invite_id;
            $http({
                method: 'post',
                url: vm.http + 'add_userinfo',
                data: vm.data
            })
                .then(function successCallback(res) {

                    if (res.data.code == 0) {
                        if (sessionStorage.witness_id) {
                            // window.location.href = $rootScope.http_main + '/#/detail/invite_friend';
                            $rootScope.alert(res.data.msg, function () {
                                $state.go('detail.invite_friend')
                            })
                        } else if (sessionStorage.regist == 1) {
                            window.location.href = $rootScope.http_main + '/#/main/home';
                            // alert('house')
                        } else {
                            $rootScope.alert(res.data.msg, function () {
                                // $state.go('main.home')
                                history.go(-1);
                            })
                        }
                    }
                    else {
                        $rootScope.alert(res.data.msg,function () {
                            window.location.href = $rootScope.http_main + '/#/main/home';
                        })
                    }

                }, function errorCallback(res) {// 请求失败执行代码
                    $rootScope.alertError()
                });
        };
        // 获取邀请人信息
        if (localStorage.invite_id) {
            $http({
                method: 'post',
                url: sessionStorage.http + 'get_userinfo',
                data: {
                    user_id: localStorage.invite_id
                }
            })
                .then(function successCallback(res) {
                    console.log(res.data);
                    if (res.data.code == 0) {
                        vm.invite_name=res.data.data.user.name;
                        vm.invite_mobile=res.data.data.user.mobile;
                        vm.invite_mobile=vm.invite_mobile.substr(0, 3) + '****' + vm.invite_mobile.substr(7, 11);
                    }
                }, function errorCallback(res) {// 请求失败执行代码
                    // $rootScope.alertError()
                });
        }
        // 获取用户信息
        if ($rootScope.id) {
            $http({
                method: 'post',
                url: sessionStorage.http + 'get_userinfo',
                data: {
                    user_id: $rootScope.id
                }
            })
                .then(function successCallback(res) {
                    console.log(res.data);
                    if (res.data.code == 0) {
                        vm.note = res.data.data.user.note;
                        vm.is_com = res.data.data.user.is_com;
                        if (res.data.data.user.is_com == 1) {
                            // window.location.href = $rootScope.http_main + '/#/registered';
                            // $state.go('registered', {}, {reload: true});
                            $rootScope.alert('您已注册',function () {
                                $state.go('main.home')
                            });
                        }
                    }
                }, function errorCallback(res) {// 请求失败执行代码
                    // $rootScope.alertError()
                });
        } else {
            //设置注册页跳转参数
            sessionStorage.regist = 1;
            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxa5e230c798925e6e&redirect_uri=' + $rootScope.http_main + '&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect';
        }
    });